package cn.thrjit.controller.manager;

import cn.thrjit.autoconfigure.CosAutoConfiguration;
import cn.thrjit.pojo.domain.Systemdictionary;
import cn.thrjit.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.InputStream;

@Tag(name = "Cos文件上传",description = "Cos文件上传控制器类")
@RestController
@RequestMapping("/cos")
public class CosController {
    @Autowired
    private CosAutoConfiguration cosAutoConfiguration;
    @Operation( summary= "文件上传",description = "文件上传的接口")
    @Parameter(name = "file",description = "文件的对象",required = true)
    @PostMapping("/upload/{floder}")
    public R<String> uploadFile(@PathVariable("floder") String floder, MultipartFile file){
        try {
            // 获取文件本身
            InputStream inputStream = file.getInputStream();
            String filename = file.getOriginalFilename();
            String filePath = cosAutoConfiguration.uploadUrl(inputStream, floder, filename);
            return R.success(filePath);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("上传失败");
        }
    }

}
